<?php

namespace App\Http\Controllers;

use App\User;
use Illuminate\Http\Request;
use App\Http\Requests;
use Hash;
use App\Rule;

class UserController extends Controller
{


    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Http\Response
     */

    public function checkName($admin) {

        $count=User::where('email',$admin)->count();

        if($count > 0){
            $r=true;
        }else{
            $r=false;
        }
        return response()->json($r);
    }

    public function getAll($company) {
        $user=User::where('company',$company)->get();
        return response()->json($user);
    }
    public function getCurrent($page,$company) {
        $pages=$page*10-10;
        $user=User::where('company',$company)->skip($pages)->take(10)->get();
        return response()->json($user);
    }
    public function one($id) {
        $user=User::find($id);
        return response()->json($user);
    }

    public function getCount($company) {
        $user=User::where('company',$company)->count();
        return response()->json($user);
    }

    public function add(Request $request) {
      
        $adduser = $request->input('date');
        $adduser['password'] = Hash::make('123');
        User::create($adduser);
        return response()->json('添加成功');
    }
    public function del ( Request $request) {
        $date=$request->input('date');

        foreach ($date as $d){
            User::destroy($d['id']);
        }
        return response()->json('删除成功');

    }
    public function update($id, Request $request) {
        $adduser = User::find($id);
        if($request->input('password') !='' ){
            if($request->input('password') === $request->input('password_confirmation') ){
                $adduser->password = Hash::make($request->input('password'));
            }else{
                return response()->json('密码不相同');
                dd();
            }
        }
        $adduser->name = $request->input('name');

        $adduser->save();
        Rule::where('uid',$id)->delete();
        $addr['uid']=$id;
        if ($request->input('rule')){
            $rule=$request->input('rule');
            foreach ($rule as $r){
                $addr['name']=$r;
                Rule::create($addr);
            }
        }
        return response()->json('修改成功');
    }
    public function search($word) {
        $user=User::where('name','like','%'.$word.'%')->OrWhere('email','like','%'.$word.'%')->get();
        return response()->json($user);
    }
    public function page($page,$pagesize) {
        $skip=$page*$pagesize;
        $take=$pagesize;
        $user=User::orderBy('id','desc')->skip($skip)->take($take)->get();
        return response()->json($user);
    }
    public function dateOne($date) {

        $user=User::where('created_at',$date)->get();
        return response()->json($user);
    }
    public function datemore($date) {

        $user=User::where('created_at',$date)->get();
        return response()->json($user);
    }





}
